//headers 
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
#include<cstring>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define dbge(x) cout<<#x<<"->"<<x<<"\t";
#define PRINT(x) REP(i,x.sz) dbge(x[i]);
#define OUT(x) x<<" "
const int INF = (int) 1e8;


//code of suren

int grid[100][100],visited[100];
int N,end,ans;

void dfs(int node, int weight){
  if(weight == ans) end=min(end,node);
  if(weight > ans) { ans=weight; end=node; }
  
  visited[node] = 1;
  REP(i,N) if(!visited[i] && grid[node][i]) dfs(i,weight+1);
  visited[node] = 0;
}

int main(){
  int n; int c=0;
  while( cin>>n && n){
    if(c) cout<<endl; c++;
    int start; cin>>start; 
    N=n; 
    int i,j;
    ans = 0; end = INF;
    memset(visited,0,sizeof(visited));
    memset(grid,0,sizeof(grid));
    while( cin>>i>>j && i && j ) grid[i-1][j-1]=1;
    dfs(start-1,0);
    cout<<"Case "<<c<<": The longest path from "<<start<<" has length "<<ans<<", finishing at "<<end+1<<".";
  }
  return 0;
}
